import { createRouter, createWebHistory } from 'vue-router'
import HomePage from "./views/HomePage.vue"
//import HomePage3 from "./views/HomePage3.vue"
import ArticleDetails from "./views/ArticleDetails.vue"
import FenLei from "./views/FenLei.vue"
import TagView from "./views/TagView.vue"
import FenLeiList from "./views/FenLeiList.vue"
import GuiDang from "./views/GuiDang.vue"
import HomeAdmin from "./views/BlogAdmin/HomeAdmin.vue"
import WriteArticle from "./views/BlogAdmin/WriteArticle.vue"
import ArticleManage from "./views/BlogAdmin/ArticleManage.vue"
import AdminLogin from "./views/BlogAdmin/AdminLogin.vue"
const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/', component: HomePage,
      meta: {
        title: '首页'
      }
    },
    {
      path: '/ArticleDetails/:id', component: ArticleDetails, name: 'articleDetails',
      meta: {
        title: '文章'
      }
    },
    {
      path: '/FenLei',
      component: FenLei,
      name: 'FenLei',
      meta: {
        title: '文章分类'
      }
    },
    {
      path: '/FenLeiList', 
      component: FenLeiList, 
      name: 'FenLeiList', 
    },
    {
      path: '/GuiDang', 
      component: GuiDang, 
      name: 'GuiDang', 
      meta: {
        title: '归档'
      }
    },
    {
      path: '/TagView', 
      component: TagView, 
      name: 'TagView', 
      meta: {
        title: '标签'
      }
    },
    //后台管理页面
    {
      path: '/HomeAdmin', 
      component: HomeAdmin, 
      name: 'HomeAdmin', 
      meta: {
        title: '管理员首页',
        requiresAuth: true, // 表示需要登录权限
      },
      children: [
        {
          path: 'WriteArticle',
          component: WriteArticle,
          meta: {
            title: '写文章'
          },
        },
        {
          path: 'ArticleManage',
          component: ArticleManage,
          meta: {
            title: '文章管理'
          },
        },
      ]
    },
    {
      path: '/AdminLogin', 
      component: AdminLogin, 
      name: 'AdminLogin', 
      meta: {
        title: '登录'
      }
    },
  ],

})
// router.beforeEach((to, from, next) => {//beforeEach是router的钩子函数，在进入路由前执行
//   // 鉴权逻辑
//   const isAuthenticated = checkUserAuthentication(); // 
//   if (to.meta.requiresAuth && !isAuthenticated) 
//   {
//     // 路由需要登录权限且用户未认证，则跳转到登录页面
//     next('/AdminLogin');
//   } 
//   else 
//   {
//     if (to.meta.title && typeof to.meta.title === 'string') {
//       document.title = to.meta.title;
//     }
//     next(); // 执行进入路由
//   }
// });
// function checkUserAuthentication() {
//   // 进行用户登录状态的检查，返回 true 表示已认证，false 表示未认证
//   // 这里可以根据实际情况从后端接口或本地存储获取用户登录状态信息
//   return localStorage.getItem('ToKen') ? true : false; // 通过 token 判断用户是否已登录
// }

export default router
